<?php
/**
 * Created by PhpStorm.
 * User: jim
 * Date: 2016-12-22
 * Time: 22:13
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $table = "order";

    //查询当前用户的所有订单
    public function userorder()
    {
        if(session()->has('homeuser')) {
            $id = session('homeuser');
            $ob = $this->where('user_id', $id)->get();
            if ($ob->count() > 0) {
                return $ob;
            } else {
                return null;
            }
        }else {
            return null;
        }
    }

    //查询当前用户订单状态的个数
    public function ordersta($status)
    {
        if(session()->has('homeuser')) {
            $id = session('homeuser');
            $ob = $this->where([['user_id', $id],['status', $status]])->get();
            if ($ob->count() > 0) {
                return $ob;
            } else {
                return null;
            }
        }else {
            return null;
        }
    }

    //查询当前用户完成和取消的订单
    public function orderwq()
    {
        if(session()->has('homeuser')) {
            $id = session('homeuser');
            $ob = $this->where([['user_id', $id],['status', '5']])->orwhere([['user_id', $id],['status', '6']])->get();
            if ($ob->count() > 0) {
                return $ob;
            } else {
                return null;
            }
        }else {
            return null;
        }
    }

    //取消用户选中的订单
    public function cancelorder($id)
    {
        $bo = $this->where('id',$id)->update(['status' => 6]);
        if($bo){
            return $bo;
        }else{
            return null;
        }
    }

    //删除用户选中的订单
    public function deleteorder($id)
    {
        $bo = $this->where('id',$id)->update(['status' => 7]);
        if($bo){
            return $bo;
        }else{
            return null;
        }
    }

    //查询单条订单的信息
    public function firstorder($id)
    {
        $bo = $this->where('id',$id)->first();
        if($bo){
            return $bo;
        }else{
            return null;
        }
    }

    //查询所有订单
    public function getorder()
    {
        $bo = $this->get();
        if($bo->count() >0){
            return $bo;
        }else{
            return null;
        }
    }

    //查询商家所有订单
    public function storeOrder()
    {
        $id = session('storelist')->id;
        $bo = $this->where('store_id',$id)->get();
        if($bo->count() >0){
            return $bo;
        }else{
            return null;
        }
    }

    //查询被删除的订单
    public function recycleorder()
    {
        $bo = $this->where('status',7)->get();
        if($bo){
            return $bo;
        }else{
            return null;
        }
    }

    //正向绑定order_info
    public function order_info()
    {
        return $this->hasMany('App\Models\Order_info');
    }
    public function order_time()
    {
        return $this->hasMany('App\Models\Order_time');
    }
}